Can`t access outer class modifiers (java)?

ZeroButton is not a member of outer class ( MainFrame ), it's a local variable in MainFrame constructor.

ZeroButton is not a member of outer class (MainFrame), it's a local variable in MainFrame constructor. Try something like this public class MainFrame extends JFrame { private final JButton zeroButton; MainFrame() { zeroButton = new JButton("0"); PS I'm also not sure if you're supposed to compare controls with ==. (never used swing).

Since it is a class level variable, it need not be final. Any instance variable will be accessible to anonymous/inner classes – Nivas Oct 10 '10 at 13:34 (embarassed. ) Thank you :) – w4j3d Oct 10 '10 at 13:34 2 @Nivas I know that both private and final are optional, I just often apply them 'by default' to all members :) – Nikita Rybak Oct 10 '10 at 13:35 I just often apply them 'by default' to all members: I think you mean memebers that are GUI elements, like JButton, and perhaps not all members by default.

I apply private to all, but final was a bit strange. – Nivas Oct 10 '10 at 13:42 @Nivas It really depends on domain. I don't work with GUI in Java, so can't tell about it, but if you work in multi-threaded environment it's usually a good idea (unless you see specific reason for a field to be changeable).

Helps with thread-safety. – Nikita Rybak Oct 10 '10 at 14:05.

You are unable to access it because the button is a local variable in another method. You have two options: 1. Make the button in a instance variable (a class level variable). See Nikita's answer.

Have the handler as an anonymous implementation, in the constructor: MainFrame() { final JButton zeroButton = new JButton("0"); add(zeroButton); Handler handler = new Handler(); zeroButton. AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == **zeroButton**) JOptionPane. ShowMessageDialog(null, "hello there!

"); } }); } In this case, the variable should be final because only final local variables are accessible to inner classes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions